<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="webjars/jquery/3.6.4/jquery.min.js"></script>
    <link href="webjars/bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
    <script>
         $(function (){
              let id = $("[name='id']").val();
               //alert(id);
              $.getJSON(
                  "findUser",
                  {id:id},
                  function (data){
                       console.log(data);
                       let roles = data.roles;
                       let user=data.user;
                       $("[name='name']").val(user.name);
                       $("[name='pwd']").val(user.pwd);
                       $("[name='in_time']").val(user.in_time);
                       $("[name='age']").val(user.age);
                       $("[name='address']").val(user.address);

                       for(var i in roles) {
                           $("#roles").append("<input type='checkbox' name='ids' value='"+roles[i].rid+"'>"+roles[i].rname);
                       }
                       for(var i in user.roles){
                           $("[name='ids'][value='"+user.roles[i].rid+"']").prop("checked",true);
                       }
                  }
              )
             //修改的提交
             $(".btn-success").click(function (){
                 $.getJSON(
                     "save",
                     $("form").serialize(),
                     function (obj){
                         if(obj){
                             alert("修改成功！")
                             location='list'
                         }
                     }
                 )
             })

         })
    </script>
</head>
<body>
          <div class="container">
               <form>
                   <input type="hidden"  name="id" th:value="${id}">
                    账号：<input type="text" name="name" ><br>
                    密码:<input type="password" name="pwd"><br>
                    年龄：<input type="text" name="age"><br>
                    入职时间：<input type="text" name="in_time"><br>
                    地址：<input type="text" name="address"><br>
                     角色：<span id="roles">
                         </span>
               </form>
              <button class="btn btn-success">修改</button>
          </div>

</body>
</html>
